package com.johnson.news.activity;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.johnson.news.R;
import com.johnson.news.databinding.ActivityRegisterBinding;
import com.johnson.news.event_msg.LoginMsg;
import com.johnson.news.model.bean.BaseResponseData;
import com.johnson.news.model.bean.DataUserInfo;
import com.johnson.news.model.remote.RemoteDataSource;
import com.johnson.news.model.util.NewsPreference;
import com.johnson.news.util.Utils;
import com.youmbe.library.BaseBindingActivity;
import com.youmbe.library.util.MyToast;

import org.greenrobot.eventbus.EventBus;

import static com.johnson.news.model.bean.BaseResponseData.response_ok;

public class RegisterActivity extends BaseBindingActivity<ActivityRegisterBinding> {
    boolean isRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isRegister = getIntent().getBooleanExtra("register", true);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void init() {
        dataBinding.setOnClickListener(onClickListener);
        if(!isRegister) {
            dataBinding.registerTopbar.setTitle(getString(R.string.find_pwd_title));
            dataBinding.pwdEditLayout.setHint(getString(R.string.find_pwd_input));
            dataBinding.confirmPwdEditLayout.setHint(getString(R.string.find_pwd_ensure));
            dataBinding.regBtnI.setText(getString(R.string.find_pwd_ok));
            dataBinding.loginNow.setVisibility(View.GONE);
        } else {
            dataBinding.pwdEditLayout.setHint(getString(R.string.register_password_hint));
            dataBinding.confirmPwdEditLayout.setHint(getString(R.string.register_password_2_hint));
        }
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.acquire_code:
                    getVerifyCode();
                    break;
                case R.id.login_now:
                    startActivity(LoginActivity.class, null);
                    finish();
                    break;
                case R.id.reg_btn_i:
                    register();
                    break;
            }
        }
    };

    boolean checkPhone() {
        if(TextUtils.isEmpty(dataBinding.phoneEdit.getText())) {
            dataBinding.phoneEdit.setError(getString(R.string.login_null_phone));
            return false;
        }
        if(!Utils.verifyPhone(dataBinding.phoneEdit.getText().toString())) {
            dataBinding.phoneEdit.setError(getString(R.string.login_invalid_phone));
            return false;
        }

        return true;
    }

    void register() {
        if(!checkPhone())
            return;
        if(TextUtils.isEmpty(dataBinding.codeEdit.getText())) {
            dataBinding.codeEdit.setError(getString(R.string.register_null_verify_code));
            return;
        }

        String pwd = dataBinding.pwdEdit.getText().toString(),
                confirmPwd = dataBinding.confirmPwdEdit.getText().toString();
        if(TextUtils.isEmpty(pwd)) {
            dataBinding.pwdEdit.setError(getString(R.string.login_null_password));
            return;
        }
        if(TextUtils.isEmpty(confirmPwd)) {
            dataBinding.confirmPwdEdit.setError(getString(R.string.login_null_password));
            return;
        }

        if(!confirmPwd.equals(pwd)) {
            dataBinding.confirmPwdEdit.setError(getString(R.string.register_diff_password));
            return;
        }

        if(pwd.length() < 6) {
            dataBinding.pwdEdit.setError(getString(R.string.register_invalid_password));
            return;
        }

        Observable<DataUserInfo> observable;
        if(isRegister) {
            observable = RemoteDataSource.getRemoteDataSource()
                    .register(dataBinding.phoneEdit.getText().toString()
                            , dataBinding.codeEdit.getText().toString()
                            , pwd.toString(), Utils.channelId);
        } else {
            observable = RemoteDataSource.getRemoteDataSource()
                    .findPwd(dataBinding.phoneEdit.getText().toString()
                            , dataBinding.codeEdit.getText().toString()
                            , pwd.toString());
        }

        observable.subscribe(new Consumer<DataUserInfo>() {
            @Override
            public void accept(DataUserInfo dataUserInfo) throws Exception {
                if(dataUserInfo.status == response_ok) {
//                    Utils.saveUserToken(activityInstance, dataUserInfo.token);

                    NewsPreference pref = NewsPreference.getinstance(RegisterActivity.this);
                    pref.setToken(dataUserInfo.token);
                    pref.setLoginName(dataBinding.phoneEdit.getText().toString());
                    pref.setLoginState(true);

                    if(isRegister) {
                        MyToast.showToast("注册成功", activityInstance);
                        EventBus.getDefault().post(new LoginMsg(dataBinding.phoneEdit.getText().toString()));
                    } else
                        MyToast.showToast("密码找回成功。", activityInstance);
                    finish();
                } else {
                    MyToast.showToast(dataUserInfo.msg, activityInstance);
                }
            }
        });

    }

    void getVerifyCode() {
        if(!checkPhone())
            return;

        Observable<BaseResponseData> observable;
        if(isRegister) {
            observable = RemoteDataSource.getRemoteDataSource()
                    .getVerifyCodeOfRegister(dataBinding.phoneEdit.getText().toString());
        } else {
            observable = RemoteDataSource.getRemoteDataSource()
                    .getVerifyCodeOfFindPwd(dataBinding.phoneEdit.getText().toString());
        }

        observable.subscribe(new Consumer<BaseResponseData>() {
            @Override
            public void accept(BaseResponseData baseResponseData) throws Exception {
                if(baseResponseData.status != BaseResponseData.response_ok) {
                    MyToast.showToast(baseResponseData.msg, activityInstance);
                } else {
                    dataBinding.acquireCode.startTiming();
                }
            }
        });
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_register;
    }
}
